import 'package:uuid/uuid.dart';
import 'package:flutter/material.dart';

enum RecordType {
  feeding,
  diaper,
  sleep,
  temperature,
  medicine,
  vaccine,
  weight,
  height,
  milestone,
  food,
  activity,
  doctor,
  mood,
  custom;

  String get label {
    switch (this) {
      case RecordType.feeding:
        return '喂奶';
      case RecordType.diaper:
        return '换尿布';
      case RecordType.sleep:
        return '睡眠';
      case RecordType.temperature:
        return '体温';
      case RecordType.medicine:
        return '用药';
      case RecordType.vaccine:
        return '疫苗';
      case RecordType.weight:
        return '体重';
      case RecordType.height:
        return '身高';
      case RecordType.milestone:
        return '里程碑';
      case RecordType.food:
        return '辅食';
      case RecordType.activity:
        return '活动';
      case RecordType.doctor:
        return '就医';
      case RecordType.mood:
        return '心情';
      case RecordType.custom:
        return '自定义';
    }
  }

  IconData get icon {
    switch (this) {
      case RecordType.feeding:
        return Icons.local_cafe;
      case RecordType.diaper:
        return Icons.cleaning_services;
      case RecordType.sleep:
        return Icons.nightlight_round;
      case RecordType.temperature:
        return Icons.thermostat;
      case RecordType.medicine:
        return Icons.medication;
      case RecordType.vaccine:
        return Icons.vaccines;
      case RecordType.weight:
        return Icons.monitor_weight;
      case RecordType.height:
        return Icons.height;
      case RecordType.milestone:
        return Icons.emoji_events;
      case RecordType.food:
        return Icons.restaurant;
      case RecordType.activity:
        return Icons.directions_run;
      case RecordType.doctor:
        return Icons.local_hospital;
      case RecordType.mood:
        return Icons.mood;
      case RecordType.custom:
        return Icons.edit_note;
    }
  }

  Color get color {
    switch (this) {
      case RecordType.feeding:
        return Colors.blue;
      case RecordType.diaper:
        return Colors.green;
      case RecordType.sleep:
        return Colors.indigo;
      case RecordType.temperature:
        return Colors.orange;
      case RecordType.medicine:
        return Colors.red;
      case RecordType.vaccine:
        return Colors.teal;
      case RecordType.weight:
        return Colors.purple;
      case RecordType.height:
        return Colors.cyan;
      case RecordType.milestone:
        return Colors.amber;
      case RecordType.food:
        return Colors.deepOrange;
      case RecordType.activity:
        return Colors.lightGreen;
      case RecordType.doctor:
        return Colors.red;
      case RecordType.mood:
        return Colors.pink;
      case RecordType.custom:
        return Colors.grey;
    }
  }
}

class HealthRecord {
  final String id;
  final String babyId;
  final DateTime timestamp;
  final RecordType type;
  final Map<String, dynamic> data;
  String? note;

  HealthRecord({
    String? id,
    required this.babyId,
    required this.type,
    required this.data,
    DateTime? timestamp,
    this.note,
  }) : id = id ?? const Uuid().v4(),
       timestamp = timestamp ?? DateTime.now();

  // 从JSON转换
  factory HealthRecord.fromJson(Map<String, dynamic> json) {
    return HealthRecord(
      id: json['id'],
      babyId: json['babyId'],
      type: RecordType.values.firstWhere(
        (e) => e.toString() == 'RecordType.${json['type']}',
        orElse: () => RecordType.custom,
      ),
      timestamp: DateTime.parse(json['timestamp']),
      data: Map<String, dynamic>.from(json['data']),
      note: json['note'],
    );
  }

  // 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'babyId': babyId,
      'type': type.toString().split('.').last,
      'timestamp': timestamp.toIso8601String(),
      'data': data,
      'note': note,
    };
  }

  // 获取记录的摘要信息
  String getSummary() {
    switch (type) {
      case RecordType.feeding:
        final amount = data['amount'];
        final unit = data['unit'] ?? 'ml';
        return '喂养 $amount$unit';

      case RecordType.diaper:
        final wetDiaper = data['wet'] == true ? '尿布' : '';
        final dirtyDiaper = data['dirty'] == true ? '便便' : '';
        return '更换${wetDiaper}${dirtyDiaper}';

      case RecordType.sleep:
        final duration = data['duration'] ?? 0;
        return '睡眠 ${duration}分钟';

      case RecordType.temperature:
        final temp = data['temperature'];
        return '体温 ${temp}°C';

      case RecordType.weight:
        final weight = data['weight'];
        return '体重 ${weight}kg';

      case RecordType.height:
        final height = data['height'];
        return '身高 ${height}cm';

      case RecordType.vaccine:
        return '接种疫苗: ${data['name']}';

      case RecordType.medicine:
        return '用药: ${data['name']}';

      default:
        return data['summary'] ?? '自定义记录';
    }
  }

  // 复制对象并修改部分属性
  HealthRecord copyWith({
    String? babyId,
    RecordType? type,
    Map<String, dynamic>? data,
    DateTime? timestamp,
    String? note,
  }) {
    return HealthRecord(
      id: id,
      babyId: babyId ?? this.babyId,
      type: type ?? this.type,
      data: data ?? Map<String, dynamic>.from(this.data),
      timestamp: timestamp ?? this.timestamp,
      note: note ?? this.note,
    );
  }
}
